home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $asm1.P < prev    next >
Text File  |  1990-04-12  |  8KB  |  274 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $asm1.P */
  25.  
  26. /* **********************************************************************
  27. $asm1_export([$asm/3,$asm_PIL/2]).
  28.  
  29. $asm1_use($blist,[$append/3,$member/2,$member1/2]).
  30. $asm1_use($bio,[$writename/1,$writeqname/1,$put/1,$nl/0,$tab/1,$tell/1,
  31.     $telling/1,$told/0,$get/1,$get0/1,$see/1,$seeing/1,$seen/0]).
  32. $asm1_use($asmpass11,[$asm_pass1/6]).
  33. $asm1_use($asmpass21,[$asm_syminst/4,$asm_lookup0/3,$asm_lookup/3]).
  34. $asm1_use($read,[$read/1,_]).
  35. ********************************************************************** */
  36.  
  37.  
  38. $asm(Infile,Outfile,Opts) :- 
  39.     (($member1(v,Opts), $writename(' - assembly phase commenced'),$nl) ;
  40.      true),
  41.     $see(Infile),
  42.     $asm_getaslist(Inprog,Index, Nindex, [], 0), $seen,
  43.     $asm_pass1( Inprog, Index, Psctable, Labeltable, Ntext, Npsc), 
  44.     (($member1(v,Opts), $tab(15), $writename('pass 1 complete'),$nl,
  45.       $told) ;
  46.      true),
  47.     $tell(Outfile),
  48.     $asm_magic(3),
  49.     $asm_putnum(Npsc, 4), $asm_putnum(Ntext, 4), $asm_putnum(Nindex, 4),
  50.     $asm_pass2(Inprog, Index, Psctable, Labeltable,Opts),
  51.     $asm_mark_eot,
  52.     $told, $tell(user),
  53.     (($member1(v,Opts), $tab(15), $writename('pass 2 complete'),$nl) ;
  54.     true).
  55.  
  56. $asm_PIL(Inprog,Opts) :- /* already telling outfile */
  57.     $asm_get_index(Inprog, NInprog, Index, Nindex, [], 0),
  58.     $asm_pass1(NInprog, Index, Psctable, Labeltable, Ntext, Npsc), 
  59.     $asm_magic(3), 
  60.     $asm_putnum(Npsc, 4), $asm_putnum(Ntext, 4), $asm_putnum(Nindex, 4),
  61.     $asm_pass2(NInprog, Index, Psctable, Labeltable,Opts),
  62.     $asm_mark_eot.
  63.  
  64.  
  65. $asm_get_index([],[],[],N,_,N).
  66. $asm_get_index([Inst|Rest],Inprog,Index,Nindex,Tail,Ni) :-
  67.     $asm_index_inst(Inst,Size) ->
  68.         (N is Ni + Size,
  69.          Index = [Inst|Rindex],
  70.          $asm_get_index(Rest,Inprog,Rindex,Nindex,Tail,N)
  71.         );
  72.         (Inprog = [Inst|Rprog],
  73.          $asm_get_index(Rest, Rprog, Index, Nindex, Tail, Ni)
  74.         ).
  75.  
  76. /* ---------------------------------------------------------------------- */
  77.  
  78. $asm_getaslist(Insts,Index,Nindex,Tail,Ni) :-
  79.     $read(Inst0),
  80.     (Inst0 ?= end_of_file ->
  81.         (Insts = Tail, 
  82.            Index = Tail, Nindex = Ni) ;
  83.          ($asm_index_inst(Inst0,Size) ->
  84.               (N is Ni + Size,
  85.                Index = [Inst0|Rindex],
  86.                  $asm_getaslist(Insts,Rindex,Nindex,Tail,N)) ;
  87.               (Insts = [Inst0 | Rinsts],
  88.                  $asm_getaslist(Rinsts,Index,Nindex,Tail,Ni))
  89.         )
  90.     ).
  91.  
  92.  
  93. /*    $asm_pass2 takes as input a program containing symbolic labels, 
  94. structure symbols and constants and the symbol tables and returns
  95. a program in which all symbols have been replaced by their byte
  96. code offsets of PSC indices. Also, for "internal" predicates, i.e.
  97. those which are not exported, static linking is carried out as far as
  98. possible.                                */
  99.  
  100. $asm_pass2(Prog,Index,Csym,Lsym,Opts) :-
  101.     $asm_symbol(Csym),
  102.     $asm_pass2a(Prog,Csym,Lsym,Opts),
  103.     $asm_index(Index, Csym, Lsym).
  104.  
  105. $asm_index(Index,Csym,Lsym) :-
  106.     $member(Inst, Index),
  107.     $asm_proc_index(Inst, Csym, Lsym).
  108. $asm_index(_,_,_).
  109.  
  110. $asm_proc_index(pred(Label, Num), Csym, Lsym) :-
  111.     Label = (P, N, _), 
  112.     $asm_lookup((P, N, _), Csym, Index),
  113.     $asm_putnum(Index, 4), $asm_putnum(Num, 4),
  114.     !, 
  115.     fail.
  116. $asm_proc_index(arglabel(T,Val,Label), Csym, Lsym) :-
  117.     (T ?= c ->
  118.          $asm_lookup((Val,0,_), Csym, Nval) ;
  119.          (T ?= s ->
  120.               (Val = (Str, Arity),
  121.              $asm_lookup((Str, Arity, _), Csym, Nval)
  122.           ) ;
  123.            Nval = Val
  124.          )
  125.     ),
  126.     $asm_lookup((Label, L), Lsym, _),
  127.     $writename(T),
  128.     (((T ?= i; T ?= c; T ?= s),
  129.      (integer(Nval) -> $asm_putnum(Nval,4) ; $write4(Nval)));
  130.      true
  131.     ),
  132.     $asm_putnum(L, 4),
  133.     !,
  134.     fail.
  135.  
  136. $asm_pass2a(Prog,Csym,Lsym,Opts) :-
  137.     $member(Inst,Prog),
  138.     $asm_process_pil_inst(Inst,Csym,Lsym,Opts).
  139. $asm_pass2a(_,_,_,_).
  140.  
  141. $asm_process_pil_inst(label((_,_,_)),_,_,_) :-
  142.     !,
  143.     fail.
  144. $asm_process_pil_inst(call(P,A,N),Csym,Lsym,Opts) :-
  145.     !,
  146.     $asm_proc_call(P,A,N,Csym,Lsym,Inst,Opts),
  147.     $asm_emit(Inst),
  148.     !,
  149.     fail.
  150. $asm_process_pil_inst(execute(Pred),Csym,Lsym,Opts) :-
  151.     !,
  152.     $asm_proc_exec(Pred,Csym,Lsym,Inst,Opts),
  153.     $asm_emit(Inst),
  154.     !,
  155.     fail.
  156. $asm_process_pil_inst(Inst,Csym,Lsym,Opts) :-
  157.     $asm_syminst(Inst, NInst, Csym, Lsym),
  158.     $asm_emit(NInst),
  159.     !,
  160.     fail.
  161.  
  162. $asm_proc_call(Pred,Arity0,Nv,_,Lsym,Instr,Opts) :-
  163.     $not_member1(t,Opts),
  164.     $asm_lookup0( ((Pred,Arity0,_),EPaddr), Lsym, _),
  165.     (Arity0 < 0 ->
  166.          Arity1 = 0 ;     /* no regs being used here */
  167.          Arity1 = Arity0
  168.     ),
  169.     Instr = calld(EPaddr,Arity1,Nv),
  170.     !.
  171. $asm_proc_call(Pred,Arity,Nv,Csym,_,call(PSC_Index,Arity,Nv),Opts) :-
  172.     $asm_lookup( (Pred,Arity,_), Csym, PSC_Index).
  173.  
  174. $asm_proc_exec((Pred,Arity),_,Lsym,jump(EPaddr),Opts) :-
  175.     $not_member1(t,Opts),
  176.     $asm_lookup0( ((Pred,Arity,_),EPaddr), Lsym, _),
  177.     !.
  178. $asm_proc_exec((Pred,Arity),Csym,_,execute(PSC_Index),Opts) :-
  179.     $asm_lookup( (Pred,Arity,_), Csym, PSC_Index).
  180.  
  181. $asm_emit(Inst) :-
  182.     functor(Inst,Name,_),
  183.     $opcode(Name,Opcode,Type),
  184.     $asm_putnum(Opcode, 2), 
  185.     $asm_emit_operands(Type,Inst).
  186.  
  187. :- mode $asm_emit_operands(c,c).
  188.  
  189. $asm_emit_operands(p, _) :-
  190.     $asm_putnum(0,2).
  191. $asm_emit_operands(pss, Inst) :-
  192.     $asm_putnum(0, 2),
  193.     arg(1,Inst,X), $asm_putnum(X, 2),
  194.     arg(2,Inst,Y), $asm_putnum(Y, 2).
  195. $asm_emit_operands(pl, Inst) :-
  196.     $asm_putnum(0, 2),
  197.     arg(1,Inst,I), $asm_putnum(I, 4).
  198. $asm_emit_operands(s,Inst) :-
  199.     arg(1,Inst,X), $asm_putnum(X, 2).
  200. $asm_emit_operands(sss,Inst) :-
  201.     arg(1,Inst,X), $asm_putnum(X, 2),
  202.     arg(2,Inst,Y), $asm_putnum(Y, 2),
  203.     arg(3,Inst,Z), $asm_putnum(Z,2).
  204. $asm_emit_operands(sl,Inst) :-
  205.     arg(2,Inst,X), $asm_putnum(X,2),
  206.     arg(1,Inst,Y), $asm_putnum(Y,4).
  207. $asm_emit_operands(as,Inst) :-
  208.     arg(1,Inst,X), $asm_putnum(X,2),
  209.     arg(2,Inst,Y), $asm_putnum(Y,4).
  210. $asm_emit_operands(saa,Inst) :-
  211.     arg(1,Inst,R), $asm_putnum(R,2),
  212.     arg(2,Inst,A1), $asm_putnum(A1,4),
  213.     arg(3,Inst,A2), $asm_putnum(A2,4).
  214. $asm_emit_operands(bba,Inst) :-
  215.     arg(2,Inst,N), $asm_putnum(N,1),
  216.     arg(3,Inst,B), $asm_putnum(B,1),
  217.     arg(1,Inst,I), $asm_putnum(I,4).
  218. $asm_emit_operands(sf,Inst) :-
  219.     arg(2,Inst,R), $asm_putnum(R,2),
  220.     arg(1,Inst,F), $write4(F).
  221. $asm_emit_operands(pf,Inst) :-
  222.     $asm_putnum(0,2),
  223.     arg(1,Inst,F), $write4(F).
  224.  
  225.  
  226. $asm_magic(N) :-
  227.     $asm_putnum(17, 1),
  228.     $asm_putnum(18, 1),
  229.     $asm_putnum(19, 1),
  230.     $asm_putnum(N, 1).
  231.  
  232. $asm_index_inst(pred(_,_),8).
  233. $asm_index_inst(arglabel(T,_,_),N) :-
  234.     ($asm_index_inst1(T,N), !) ;
  235.     N = 5.
  236.  
  237. $asm_index_inst1(i,9).
  238. $asm_index_inst1(c,9).
  239. $asm_index_inst1(s,9).
  240.  
  241. /* $asm_symbol outputs the PSC table in byte file header format */
  242.  
  243. $asm_symbol(Symtab) :- $member(Sym, Symtab), $asm_putsym(Sym), fail.
  244. $asm_symbol(_).
  245. $asm_putsym((String, Arity, Value)) :-
  246.     $asm_putnum(Value, 4),
  247.     $asm_putnum(Arity, 1),
  248.     $conlength(String,L),
  249.     $asm_putnum(L,1),
  250.     $writename(String),
  251.     !.
  252.  
  253. /*    Putnum(Number, Length) will write Number as a binary number
  254. which will be Length bytes long */
  255.  
  256. $asm_putnum(Num,NBytes) :-
  257.     NBytes > 1 ->
  258.         (Byte is Num /\ 255,
  259.          Rest is Num >> 8,
  260.          N is NBytes - 1,
  261.          $asm_putnum(Rest,N),
  262.          $put(Byte)
  263.         ) ;
  264.         (Num < 256, $put(Num)).
  265.  
  266. $asm_mark_eot :-
  267.     $opcode( endfile, X ), 
  268.     $asm_putnum(X,2),
  269.     $asm_putnum(0,2),
  270.     $asm_putnum(0,4).
  271.  
  272.  
  273. /* end $asm1.P ***************************************************/
  274.